<!--智慧消防V1.0-edit页面-->
<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                <div style="text-align: center;margin-bottom: 30px">
                    设备巡检信息{{createStatus?'新增':'更新'}}
                </div>
                <el-form :model="inspectionForm" :rules="inspectionFormRules" ref="inspectionForm" label-width="100px">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="所属主体" prop="enterpriseId" v-if="enterPriseHidden">
                            <el-select v-model="inspectionForm.enterpriseId"  placeholder="请选择主体" style="width: 100%">
                                <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="设备" prop="equipmentId">
                            <el-select v-model="inspectionForm.equipmentId" placeholder="请选择设备信息" style="width: 100%" @change="getInspectionUserList">
                                <el-option v-for="(item,index) in equipmentList" :key="index"  :value="item.ucId" :label="item.disPlayName"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="巡检人员" prop="inspectionUser">
                            <el-select v-model="inspectionForm.inspectionUser" placeholder="请选择巡检人员" style="width: 100%">
                                <el-option v-for="(item,index) in inspectionUserList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="巡检时间" prop="inspectionTime">
                            <el-date-picker
                                    v-model="inspectionForm.inspectionTime"
                                    type="datetime"  style="width: 100%"
                                    placeholder="选择巡检时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="设备状态" prop="equipmentStatus">
                            <el-select v-model="inspectionForm.equipmentStatus" placeholder="请选择设备状态" style="width: 100%">
                                <el-option  :value="0" label="正常"></el-option>
                                <el-option  :value="1" label="损坏"></el-option>
                                <el-option  :value="2" label="停机"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="维护状态" prop="maintainStatus">
                            <el-switch v-model="inspectionForm.maintainStatus" active-text="需要维护" inactive-text="不需维护"></el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                        <el-form-item label="维护意见" prop="disposalOpinions">
                            <el-input type="text" v-model="inspectionForm.disposalOpinions" placeholder="请输入维护意见" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="巡检描述" prop="inspectionRemark">
                            <el-input type="textarea" :rows="2" placeholder="请输入巡检描述" v-model="inspectionForm.inspectionRemark">
                            </el-input>
                        </el-form-item>
                    </el-col>

                </el-form>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <div style="text-align: center" class="dialog-footer">
                        <el-button @click="cancelInspection">取 消</el-button>
                        <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createInspection">创 建</el-button>
                        <el-button :loading="addLoading" v-else type="primary" @click="updateInspection">修 改</el-button>
                    </div>
                </el-col>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {getEquipmentList, getInspectionUserList, getInspectionVo,addInspection,updateInspection} from "@/api/equipment";
import {getInEnterpriseList} from "@/api/enterprise";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";


export default {
    name: "inspectionEdit",
    data(){
        return{
            inspectionForm:{},
            inspectionFormRules:{

            },
            equipmentList:[],
            inspectionUserList:[],
            addLoading:false,
            enterpriseDominantList:[],
        }
    },
    computed:{
        createStatus(){
            return this.$route.params.ucId =="0"
        },
      enterPriseHidden(){
        let roleCode = this.$store.getters.roleCode;
        return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
      },
    },
    created() {
        this.getInspectionUserList()
        this.getEquipmentList()
        this.getInEnterpriseDominantList()
        if (!this.createStatus){
            this.getInspectionVo().then(res=>{
                this.inspectionForm = res.data
                this.getInspectionUserList(res.data.equipmentId)
            }).catch(error=>{

            })
        }
    },
    methods:{
       getInspectionVo(){
           return new Promise((resolve, reject) => {
               getInspectionVo({ucId:this.$route.params.ucId}).then(res=>{
                   resolve(res)
               }).catch(error=>{
                    reject(error)
               })
           })
       },
        getEquipmentList(){
            getEquipmentList({deleted:false}).then(res=>{
                console.log(res.data)
                this.equipmentList = res.data
            }).catch(error=>{

            })
        },
        getInspectionUserList(val){
            getInspectionUserList({ucId:val}).then(res=>{
                this.inspectionUserList = res.data
            }).catch(error=>{

            })
        },
        cancelInspection(){
          window.history.back()
        },
        createInspection(){
            this.addLoading = true
            this.$refs['inspectionForm'].validate(valid => {
                if (valid){
                    addInspection(this.inspectionForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加设备巡检信息成功"
                        })
                        this.cancelInspection()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateInspection(){
            this.addLoading = true
            this.$refs['inspectionForm'].validate(valid => {
                if (valid){
                    updateInspection(this.inspectionForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "修改设备巡检信息成功"
                        })
                        this.cancelInspection()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        }
    }
}
</script>

<style scoped>

</style>